from plone.app.layout.viewlets.common import LogoViewlet
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
import random


class LogoViewlet(LogoViewlet):
    render = ViewPageTemplateFile('tp/logo.pt')

    def getPath(self):
        return self.context.getPhysicalPath()[1]+'/gallery'

    def getPics(self):
        allpics = self.context.portal_catalog.searchResults(portal_type='Image',
                                                        sort_on="Date",
                                                        sort_order='reverse',
                                                        path=self.context.getPhysicalPath()[1]+'/gallery')
        #Sacando la imagen principal
        portadas=['mjn.jpg','roco.jpg','rdmc.jpg','cdiriangen.jpg','portada.jpg']
        first = filter(lambda item: item.id in portadas, allpics)

        picObjects = []
        if (len(first)>0):
            picObjects.append(first[0].getObject())

        #Agregando el resto de imagenes
        for picture in allpics:
            if len(first)<=0 or first[0].getObject()!=picture.getObject():
                picObjects.append(picture.getObject())

        
        #picObjects.remove(first[0].getObject())
        #Ordenamiento aleatorio de las imagenes menos de la primera
        
        random.shuffle(picObjects[1:])
        return picObjects[:4]
